package com.souku.finance.application.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.souku.finance.application.R;
import com.souku.finance.application.bean.RedpacketBean;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.widget.MyTextView;

import java.util.List;

/**
 * Created by Android on 2016/12/20.
 */

public class RedpacketSelectAdapter extends BaseAdapter {

    private List<RedpacketBean.ListBean> redlist;
    private RedpacketBean.ListBean selectBean;

    public RedpacketSelectAdapter(List<RedpacketBean.ListBean> redlist, RedpacketBean.ListBean bean) {
        this.redlist = redlist;
        this.selectBean = bean;
    }

    @Override
    public int getCount() {
        return redlist.size();
    }

    @Override
    public Object getItem(int position) {
        return redlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder = null;

        if (view == null || !(view.getTag() instanceof ViewHolder)) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_redpacket, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        RedpacketBean.ListBean bean = redlist.get(position);
        viewHolder.tvMoney.setText(MyTextUtil.setMoneyToElement(bean.getMoney()));
        viewHolder.tvName.setText(bean.getName());
        viewHolder.checkbox.setVisibility(View.VISIBLE);
        if (selectBean == null || !bean.getId().equals(selectBean.getId())) {
            viewHolder.checkbox.setChecked(false);
        } else {
            viewHolder.checkbox.setChecked(true);
        }
        viewHolder.tvDate.setText(MyTextUtil.changeTimeFormat(bean.getEndTime()));
        if (bean.getMinThreshold().equals("0")) {
            viewHolder.tvMinmoney.setVisibility(View.GONE);
        } else {
            viewHolder.tvMinmoney.setVisibility(View.VISIBLE);
            viewHolder.tvMinmoney.setText("投资满" + MyTextUtil.setMoneyToElement(bean.getMinThreshold()) + "元可用");
        }
        if (bean.getThresholdDealDuration().equals("0")) {
            viewHolder.tvMindate.setVisibility(View.GONE);
        } else {
            viewHolder.tvMindate.setVisibility(View.VISIBLE);
            viewHolder.tvMindate.setText(bean.getThresholdDealDuration() + "天以上项目可用");
        }
        return view;
    }

    static class ViewHolder {
        protected MyTextView tvMoney;
        protected TextView tvName;
        protected TextView tvMinmoney;
        protected TextView tvMindate;
        protected CheckBox checkbox;
        protected TextView tvDate;

        ViewHolder(View rootView) {
            initView(rootView);
        }

        private void initView(View rootView) {
            tvDate = (TextView) rootView.findViewById(R.id.tv_date);
            tvMoney = (MyTextView) rootView.findViewById(R.id.tv_money);
            tvName = (TextView) rootView.findViewById(R.id.tv_name);
            tvMinmoney = (TextView) rootView.findViewById(R.id.tv_minmoney);
            tvMindate = (TextView) rootView.findViewById(R.id.tv_mindate);
            checkbox = (CheckBox) rootView.findViewById(R.id.checkbox);
        }
    }

    public void setChanged(RedpacketBean.ListBean selectBean){
        this.selectBean=selectBean;
        notifyDataSetChanged();
    }

}
